package com.play.ballen.mall.vm

import androidx.lifecycle.MutableLiveData
import com.play.ballen.mall.entity.MallRecordInfo
import com.play.ballen.network.ApiRepository
import com.play.ballen.network.BallenRxHelper
import com.yuven.appframework.busevent.SingleLiveEvent
import com.yuven.appframework.network.RxSchedulerHelper
import com.yuven.appframework.util.addToDisposable
import com.yuven.baselib.component.viewmodel.BaseViewModel
import com.yuven.baselib.utils.Logger

open class MallRecordVM : BaseViewModel() {

    protected val model = ApiRepository()

    var page = 1
    private val pageSize = 20

    val listRecord = MutableLiveData<List<MallRecordInfo>?>()
    val actionGetListFailure = SingleLiveEvent<String>()

    fun basketballLog(type: String) {
        val map = mutableMapOf<String, Any>()
        map["page"] = page
        map["pagesize"] = pageSize
        map["type"] = type
        model.basketballLog(map)
            .onErrorResumeNext(BallenRxHelper.HttpErrorHandleFunc())
            .compose(BallenRxHelper.transformResponse())
            .compose(RxSchedulerHelper.io_main())
            .subscribe({ data ->
                listRecord.value = data
                Logger.i("MallRecordVM.basketballLog success list = $data")
            }, { throwable ->
                actionGetListFailure.value = throwable.message
                Logger.i("MallRecordVM.basketballLog error,msg = ${throwable.message}")
            }).addToDisposable(compositeDisposable)
    }

}